import { showToast } from '../helper/utils.js'
import storage from '@system.storage'
import fetch from '@system.fetch'
import { API_TTS} from '../const/server.js'
import { getTTSReuqestData, writeBase64ToAudioFile, genUniqueId} from '../helper/utils.js'
export const VOICE_TYPE_OBJECT_ARRAY = [
  { name: '天才童声', voice_type: 'zh_male_tiancaitongsheng_mars_bigtts' },
  { name: '奶气萌娃', voice_type: 'zh_male_naiqimengwa_mars_bigtts' },
  { name: '猴哥', voice_type: 'zh_male_sunwukong_mars_bigtts'},
  { name: '熊二', voice_type: 'zh_male_xionger_mars_bigtts' },
  { name: '佩奇猪', voice_type: 'zh_female_peiqi_mars_bigtts' },
  { name: '少儿故事', voice_type: 'zh_female_shaoergushi_mars_bigtts' },
  { name: '樱桃丸子', voice_type: 'zh_female_yingtaowanzi_mars_bigtts' },
  { name: '俏皮女声', voice_type: 'zh_female_qiaopinvsheng_mars_bigtts' },
  { name: '萌丫头', voice_type: 'zh_female_mengyatou_mars_bigtts' },
]

export const SYSTEM_ACTION_NAME_ARRY = VOICE_TYPE_OBJECT_ARRAY.map(item => item.name)

export function queryVoiceType(name) {
  for (let i = 0; i < VOICE_TYPE_OBJECT_ARRAY.length; i++) {
    if (VOICE_TYPE_OBJECT_ARRAY[i].name === name) {
      return VOICE_TYPE_OBJECT_ARRAY[i].voice_type;
    }
  }
  return 'zh_male_tiancaitongsheng_mars_bigtts';
}

export function getStorageCurrVoiceType(self) {
  storage.get({
    key: 'voice_name',
    default: '天才童声',
    success: (data) => {
      console.log('getStorageCurrVoiceType:' + data)
      // self.$app.setAppData('voice_type', data)
      self.$app.globalVoiceName = data
    },
    fail: (code) => {
      console.log(`storage get fail, code = ${code}`)
    },
  })
}

export function setCurrVoiceName(self, voiceName) {
  storage.set({
    key: 'voice_name', 
    value: voiceName,
    success: () => {
      console.log('setCurrVoiceType set success')
      self.$app.globalVoiceName = voiceName
      showToast('音色成功设置为：' + voiceName)
    },
    fail: (code) => {
      showToast('音色设置失败：')
      console.log(`setCurrVoiceType storage set fail, code = ${code}`)
    },
  })
}

export function getStorageIndexVoiceSwitch(self) {
  storage.get({
    key: 'index_voice_switch',
    default: true,
    success: (data) => {
      console.log('getIndexVoiceSwitch:' + data)
      self.$app.globalIndexSwitch = data
    },
    fail: (code) => {
      console.log(`storage get fail, code = ${code}`)
    },
  })
}

export function setIndexVoiceSwitch(self, switchStatus, callback) {
  storage.set({
    key: 'index_voice_switch', 
    value: switchStatus,
    success: () => {
      console.log('setIndexVoiceSwitch set success:' + switchStatus)
      self.$app.globalIndexSwitch = switchStatus
      let defualt = '关'
      if (switchStatus) {
        defualt = '开'
      }
      callback('suc')
      showToast('首页趣味音色设置：' + defualt)
    },
    fail: (code) => {
      callback('fail')
      showToast('首页趣味音色设置失败')
      console.log(`setIndexVoiceSwitch storage set fail, code = ${code}`)
    },
  })
}

export function  generateAudio(self, strStory) {
  const voiceName = self.$app.globalVoiceName
  const voiceType = queryVoiceType(voiceName)
  console.log('generateAudio voiceName:' + voiceName)
  console.log('generateAudio voiceType:' + voiceType)
  return new Promise((resolve, reject) => {
    fetch.fetch({
      url: API_TTS,
      method: 'POST',
      responseType: 'json',
      header: {
        'Authorization': 'Bearer;BSLwS1a7bUXc34bUW55eMeA7hRisTi7D'
      },
      data: getTTSReuqestData(voiceType, strStory),
      // data: getTTSReuqestData("S_5ujGWpdj1", strStory),
      success: (response) => {
        console.log('generateAudio func suc')
        // resolve(response.data)
        console.log(response.data)
        this.loadingShow = false
        const fileUri = 'internal://files/story/' + genUniqueId()
        writeBase64ToAudioFile(response.data.data, fileUri, (status) => {
          if (status === 'success') {
            console.log('success')
            resolve(fileUri)
            // audio.src = fileUri
            // audio.play()
            // audio.onended = function() {
            //   router.back()
            // }
          } else {
            console.log('fail')
          }
        })
      },
      fail: reject
    })
  })
}

  




  